Repository navigation
Worker-safety check: flag a variable the worker never declares - #89
Merged
Merged
Conversation
A worker is rebuilt from its text in an empty runspace, so a variable of the script's, or one a .GetNewClosure() captured, read as empty with no error. New finding: UndeclaredVariable. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A worker is rebuilt from its text in a runspace that starts empty. So a worker that reads one of the script's variables, or one a
.GetNewClosure()captured, read it as empty with no error, and the safety check didn't say so:{ param($Id) "[$prefix$Id]" }.GetNewClosure()with$prefix = 'P'returned[1].UndeclaredVariable: the worker reads a variable it never declares or assigns. The message names the variable and line, and says to pass it as a job value; when the worker was made with.GetNewClosure(), it adds that this doesn't carry variables into a worker.[ref],-OutVariableand the other parameters that fill a variable (aliases, shortened names and+nameincluded),Set-Variable/New-Variablewith a literal name,Import-LocalizedData,datablocks, the kit's worker globals, and PowerShell's automatic and preference variables. Those are read once per session from a fresh runspace, plus a short list of the ones PowerShell only makes on demand ($_,$Matches,$LASTEXITCODEand so on) or that only one of 5.1 and 7 has ($IsWindows).$global:and drive-qualified names, and shared functions, which may read their caller's variables.# pk:ignoreon the line accepts a finding.Test-PkWorkerSafety's help and the README describe the rule.False-positive pass
Run against every worker in the project, on 5.1 and 7: the 111 found in the README's examples, the harness (its child scripts' included) and
Testing/, plus the 14 thatTesting/cmdlet-hangbuilds as text. No false positives: the only finding is the worker in the new tests that's meant to be flagged.Testing/readme-examples/run.ps1also passes on both.Tested
New harness section BL: 17 workers that must pass, 9 that must be flagged,
# pk:ignore, shared functions, no double finding with the$script:rule, the closure's message, and the refused run.[1].🤖 Generated with Claude Code